package com.njh.charges.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hos.entity.his.OrderCharge;
import com.hos.vo.Result;
import com.njh.charges.mapper.OrderChargeMapper;
import com.njh.charges.service.PayService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author: 陈海彬
 * @date: Created in 2024/1/6 18:26
 * @description: 订单监听器
 * @modified By:
 * @version:
 */
@Component
@Slf4j
public class MyListener {


    @Resource
    private OrderChargeMapper orderChargeMapper;

    @Resource
    private PayService payService;

    @RabbitListener(queues = "zfb_queue")
    public void fund(Message message, Channel channel) {
        try {
            Result<String> result = payService.AlipayTrade(new String(message.getBody()));
            if (result.getCode() == 200) {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
            } else {
                Thread.sleep(1000);
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param message 队列信息
     * @create by: 陈海彬
     * @description: 死信队列监听信息
     * @create time: 2024/1/6 20:13
     */
    @RabbitListener(queues = {"zfb_dlx_queue"})
    public void dlxQueueMessage(Message message, Channel channel) {
        // byte[] body = message.getBody();
        String orderId = new String(message.getBody());
        OrderCharge orderCharge = this.selectOrderChargeByOrderId(orderId);
        // log.info("消费者接收到延迟订单【{}】", orderCharge.getOrderId());
        if (orderCharge != null) {
            // 查询订单的支付状态
            if ("0".equals(orderCharge.getOrderStatus())) {
                // 将订单状态设置成已超时
                orderCharge.setOrderStatus("2");
                this.orderChargeMapper.updateById(orderCharge);
            }
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


    }

    /**
     * @param orderId 订单id
     * @return void
     * @create by: 陈海彬
     * @description: 修改订单的支付状态
     * @create time: 2024/1/6 20:38
     */
    private void updateOrderChargeStatus(String orderId) {
        LambdaUpdateWrapper<OrderCharge> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(OrderCharge::getOrderId, orderId)
                .set(OrderCharge::getOrderStatus, "2");
        this.orderChargeMapper.update(null, wrapper);
    }


    /**
     * @param orderId 订单id
     * @return com.hos.entity.his.OrderCharge
     * @create by: 陈海彬
     * @description: 查询订单的支付状态
     * @create time: 2024/1/6 20:33
     */
    private OrderCharge selectOrderChargeByOrderId(String orderId) {
        LambdaQueryWrapper<OrderCharge> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderCharge::getOrderId, orderId);
        return this.orderChargeMapper.selectOne(wrapper);
    }
}